package com.yunshang.shoppingmall.service.impl;

import com.yunshang.shoppingmall.entity.*;
import com.yunshang.shoppingmall.mapper.ConfigMapper;
import com.yunshang.shoppingmall.mapper.FinaceTotalMapper;
import com.yunshang.shoppingmall.mapper.OrderMapper;
import com.yunshang.shoppingmall.service.Finace.FinaceService;
import com.yunshang.shoppingmall.service.Finace.FinaceTotalService;
import com.yunshang.shoppingmall.service.OrderService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ConfigMapper configMapper;
    @Autowired
    FinaceTotalMapper finaceTotalMapper;
    @Autowired
    FinaceService finaceService;
    @Autowired
    FinaceTotalService finaceTotalService;

    @Override
    public int deleteByPrimaryKey(Integer orderId) {
        return 0;
    }

    @Override
    public int insert(Order record) {
        return orderMapper.insert(record);
    }

    @Override
    public int insertSelective(Order record) {
        return 0;
    }

    @Override
    public Order selectByPrimaryKey(Integer orderId) {
        return orderMapper.selectByPrimaryKey(orderId);
    }

    @Override
    public List<Order> selectOrderCheck(Date ptime) {
        return orderMapper.selectOrderCheck(ptime);
    }

    @Override
    public Order selectByNo(String orderNo) {
        return orderMapper.selectByNo(orderNo);
    }

    @Override
    public int updateByPrimaryKeySelective(Order record) {
        return orderMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<Order> selectOrderActivityOne(String userId) {
        return orderMapper.selectOrderActivityOne(userId);
    }


    @Override
    public int updateByPrimaryKey(Order record) {
       return orderMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Order> selectAll() {
        return  orderMapper.selectAll();
    }

    @Override
    public List<Order> selectByUserId(String userId) {
        return orderMapper.selectByUserId(userId);
    }

    @Override
    public List<Order> selectDate(Date t1, Date t2) {
        return orderMapper.selectDate(t1,t2);
    }

    @Override
    public int updateOrderStatus(String orderStatus, String orderNo) {
        return orderMapper.updateOrderStatus(orderStatus,orderNo);
    }

    @Override
    public int updateOrderStatus1(String orderStatus, List<String> orderList) {
        return orderMapper.updateOrderStatus1(orderStatus,orderList);
    }

    @Override
    public List<Order> selectQuery(String orderStatus, String payStatus, String deliveStatus, String keywords,String userNo) {
        return orderMapper.selectQuery(orderStatus,payStatus,deliveStatus,keywords,userNo);
    }

    @Override
    public List<Order> selectPayOrderByUserNoId(OrderToData orderToData) {
        return orderMapper.selectPayOrderByUserNoId(orderToData);
    }

    @Override
    public Order selectFirstConsume(int userNoId) {
        return orderMapper.selectFirstConsume(userNoId);
    }

    @Override
    public int upOrderFrom(List<String> nos,Date exporttime) {
        return orderMapper.upOrderFrom(nos,exporttime);
    }

    @Override
    public int updateOrderAddressId(String orderAddressId, String orderNo) {
        return orderMapper.updateOrderAddressId(orderAddressId,orderNo);
    }

    @Override
    public List<Order> selectOrderListByPayTime(OrderToData orderToData) {
        return orderMapper.selectOrderListByPayTime(orderToData);
    }

    @Override
    public Boolean successOrderPay(Map<String, Object> map) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = new Date();
        String date = sdf.format(date1);

        UserNo userNo=(UserNo)map.get("userNo");
        Order order=orderMapper.selectByNo((String)map.get("orderNo"));
        Config config=configMapper.selectByPrimaryKey(1);
        FinaceTotal finaceTotal=finaceTotalMapper.selectFinaceBalaceAll((int)map.get("userNoId"));



//        int memberUserId = 0,memberLevel = 0,ids = 0,userNoReCommendId=0;//推荐人id
//        UserRank  userRank=null;
//        UserRank recommendUserRank=null;//推荐人的等级
//        UserNo recommendUserNo=null;
//        int recommendRankId=0;
//        double recommendCommission=0.00;
//        if (userNo1 != null) {
//            memberUserId = userNo1.getId();//用户编号ID
//            memberLevel = userNo1.getRank();
//            userNoReCommendId=userNo1.getRecommend();
//            userRank=userRankService.selectByPrimaryKey(memberLevel);//用户等级
//            recommendUserNo=sysUserNoService.selectByPrimaryKey(userNoReCommendId);
//            if(recommendUserNo!=null){
//                recommendRankId=recommendUserNo.getRank();
//                recommendUserRank=userRankService.selectByPrimaryKey(recommendRankId);
//                if(recommendUserRank!=null){
//                    recommendCommission=recommendUserRank.getCommission();
//                }
//            }
//        }
//        //推荐人积分
//        double recommendBalance=0.00;
//        int recommendFId=0;
//        finaceTotal.setUserId(userNoReCommendId);
//        FinaceTotal recommendFinaceTotal = FinaceMethods.selectFinaceBalace(finaceTotal); //推荐人积分
//        if(recommendFinaceTotal!=null){
//            recommendFId=recommendFinaceTotal.getfId();
//            recommendBalance=recommendFinaceTotal.getBalance();
//        }
//
//
//        Boolean bools=true;
//        double amount;
//        Boolean strResult = cash.matches("-?[0-9]+.*[0-9]*");
//        System.out.println("strResult"+strResult);
//        if(strResult==false){
//            amount=0;
//        }else {
//            amount=Double.parseDouble(cash);
//        }
//        //分享赚
//        if(userNoReCommendId<=0){//没有推荐人默认为零散会员编号id为62240
//            userNoReCommendId=62240;
//        }
//        if(userNoReCommendId>0){//是否有推荐人
//            double orderProfit=order.getOrderProfit();//订单利润
//            double shareEaring=0.00;
//            if(recommendCommission-userRank.getCommission()>0){
//                shareEaring=orderProfit*(recommendCommission-userRank.getCommission())/100;
//            }
//            FinaceToData finaceToDataRe = new FinaceToData();
//            finaceToDataRe.setUserId(userNoReCommendId);//推荐人用户编号Id
//            finaceToDataRe.setFinaceType("888");
//            finaceToDataRe.setRemark("分享赚"+orderNo);
//            finaceToDataRe.setClassify(1);
//            finaceToDataRe.setFinaceTime(Timestamp.valueOf(date1));//时间
//            finaceToDataRe.setAmount(new BigDecimal(shareEaring));//积分
//            finaceToDataRe.setBalance(new BigDecimal(shareEaring+recommendBalance));
//            finaceToDataRe.setOpratorUId(userNoReCommendId);
//            int row1 = finaceService.insertFinaceData(finaceToDataRe);
//            if(row1<=0){
//                bools=false;
//                System.out.println("12");
//                Logger logger= LoggerFactory.getLogger("12"+orderNo);
//                //throw new RuntimeException();
//            }
//            FinaceTotal finaceTotal4=new FinaceTotal();
//            finaceTotal4.setOpratorId(userNoReCommendId);
//            finaceTotal4.setBalance(recommendBalance+shareEaring);
//            finaceTotal4.setChanggeTime(new Timestamp(System.currentTimeMillis()));
//            finaceTotal4.setOpratorUId(userNoReCommendId);
//            finaceTotal4.setUserId(userNoReCommendId);
//            int recommendF;
//            if(recommendFId>0){
//                finaceTotal4.setfId(recommendFId);
//                recommendF=finaceTotalService.updateByPrimaryKey(finaceTotal4);
//            }else {
//                recommendF=finaceTotalService.insertSelective(finaceTotal4);
//            }
//            if(recommendF<=0){
//                bools=false;
//                System.out.println("122");
//                Logger logger= LoggerFactory.getLogger("122"+orderNo);
//                //throw new RuntimeException();
//            }
//        }
//        //分享赚
//
//
//        if(amount>0){//现金支付是否大于0
//            FinaceToData finaceToData = new FinaceToData();
//            finaceToData.setUserId(userNoId);//用户编号Id
//            finaceToData.setFinaceType("777");
//            finaceToData.setRemark("充值"+orderNo);
//            finaceToData.setClassify(1);
//            finaceToData.setFinaceTime(Timestamp.valueOf(date1));//时间
//            finaceToData.setAmount(BigDecimal.valueOf(amount));//积分
//            //用户积分的余额
//            finaceToData.setBalance(BigDecimal.valueOf(finaceTotal.getBalance1() + amount));
//            finaceToData.setOpratorUId(userNoId);
//            int res = finaceService.insertFinaceData(finaceToData);
//            if(res<=0){
//                bools=false;
//                System.out.println("Classify1");
//                Logger logger= LoggerFactory.getLogger("Classify1"+orderNo);
//                //throw new RuntimeException();
//            }
//        }
//        BigDecimal amount1;
//        if(order.getRealPay()!=null&&order.getFreight()!=null){
//            Boolean strResult1 = order.getRealPay().toString().matches("-?[0-9]+.*[0-9]*");
//            Boolean strResult2 = order.getFreight().toString().matches("-?[0-9]+.*[0-9]*");
//            if(strResult1==false||strResult2==false){
//                amount1=BigDecimal.valueOf(0);
//            }else {
//                amount1=BigDecimal.valueOf(order.getRealPay()).add(order.getFreight());
//            }
//        }else {
//            amount1=BigDecimal.valueOf(0);
//        }
//        if(amount1.doubleValue()>0){//实际支付总金额
//            System.out.println("Classify1a");
//            FinaceToData finaceToData1 = new FinaceToData();
//            finaceToData1.setUserId(userNoId);//用户编号Id
//            finaceToData1.setFinaceType("111");
//            finaceToData1.setRemark("支出"+orderNo);
//            finaceToData1.setClassify(1);
//            finaceToData1.setFinaceTime(Timestamp.valueOf(date1));//时间
//            finaceToData1.setAmount(amount1);//积分
//            System.out.println(finaceTotal.toString());
//            System.out.println(finaceTotal.getBalance1());
//            finaceToData1.setBalance(BigDecimal.valueOf((finaceTotal.getBalance1() + amount) - amount1.doubleValue()));
//            finaceToData1.setOpratorUId(userNoId);
//            int res1 = finaceService.insertFinaceData(finaceToData1);
//            if(res1<=0){
//                bools=false;
//                System.out.println("Classify1b");
//                Logger logger= LoggerFactory.getLogger("Classify1b"+orderNo);
//                //throw new RuntimeException();
//            }else {
//                //更新财务余额
//                FinaceTotal finaceTotal4=new FinaceTotal();
//                finaceTotal4.setOpratorId(userNoId);
//                finaceTotal4.setBalance(((finaceTotal.getBalance1() + amount) - amount1.doubleValue()));
//                finaceTotal4.setChanggeTime(new Timestamp(System.currentTimeMillis()));
//                finaceTotal4.setOpratorUId(userNoId);
//                finaceTotal4.setUserId(userNoId);
//                finaceTotal4.setfType("1");
//                int cosRow;
//                if(finaceTotal.getfId1()>0){
//                    finaceTotal4.setfId(finaceTotal.getfId1());
//                    cosRow =finaceTotalService.updateByPrimaryKeySelective(finaceTotal4);
//                }else {
//                    cosRow =finaceTotalService.insertSelective(finaceTotal4);
//                }
//                if (cosRow <= 0) {
//                    bools=false;
//                    System.out.println("Classify1c");
//                    Logger logger= LoggerFactory.getLogger("Classify1c"+orderNo);
//                    //消费积分扣除有误，支付失败!
//                    //throw new RuntimeException();
//                }
//            }
//        }
//        if(order.getCostInter()!=null){//订单实际支付使用的消费积分
//            if(order.getCostInter().doubleValue()>0){
//                FinaceToData finaceToData2 = new FinaceToData();
//                finaceToData2.setUserId(userNoId);//用户编号Id
//                finaceToData2.setFinaceType("111");
//                finaceToData2.setRemark("支出"+orderNo);
//                finaceToData2.setClassify(4);
//                finaceToData2.setFinaceTime(Timestamp.valueOf(date1));//时间
//                BigDecimal amount2=order.getCostInter();
//                finaceToData2.setAmount(amount2);//
//                finaceToData2.setBalance(BigDecimal.valueOf((finaceTotal.getBalance4()) - amount2.doubleValue()));
//                finaceToData2.setOpratorUId(userNoId);
//                int res2 = finaceService.insertFinaceData(finaceToData2);
//                if(res2<=0){
//                    //消费积分扣除有误
//                    bools=false;
//                    System.out.println("Classify4");
//                    Logger logger= LoggerFactory.getLogger("Classify4"+orderNo);
//                    //throw new RuntimeException();
//                }else {
//                    //更新财务余额
//                    FinaceTotal finaceTotal4=new FinaceTotal();
//                    finaceTotal4.setfId(finaceTotal.getfId4());
//                    finaceTotal4.setOpratorId(userNoId);
//                    finaceTotal4.setBalance(((finaceTotal.getBalance4()) - amount2.doubleValue()));
//                    finaceTotal4.setChanggeTime(new Timestamp(System.currentTimeMillis()));
//                    finaceTotal4.setOpratorUId(userNoId);
//                    finaceTotal4.setUserId(userNoId);
//                    finaceTotal4.setfType("4");
//                    int cosRow;
//                    if(finaceTotal.getfId4()>0){
//                        cosRow =finaceTotalService.updateByPrimaryKeySelective(finaceTotal4);
//                    }else {
//                        cosRow =finaceTotalService.insertSelective(finaceTotal4);
//                    }
//                    if (cosRow <= 0) {
//                        bools=false;
//                        System.out.println("Classify4a");
//                        Logger logger= LoggerFactory.getLogger("Classify4a"+orderNo);
//                        //消费积分扣除有误，支付失败!
//                        //throw new RuntimeException();
//                    }
//                }
//            }
//        }
//        if(order.getCoupon()!=null){
//            if(order.getCoupon().doubleValue()>0){
//                FinaceToData finaceToData2 = new FinaceToData();
//                finaceToData2.setUserId(userNoId);//用户编号Id
//                finaceToData2.setFinaceType("111");
//                finaceToData2.setRemark("支出"+orderNo);
//                finaceToData2.setClassify(2);
//                finaceToData2.setFinaceTime(Timestamp.valueOf(date1));//时间
//                BigDecimal amount2=order.getCoupon();
//                finaceToData2.setAmount(amount2);//
//                finaceToData2.setBalance(BigDecimal.valueOf((finaceTotal.getBalance2()) - amount2.doubleValue()));
//                finaceToData2.setOpratorUId(userNoId);
//                int res2 = finaceService.insertFinaceData(finaceToData2);
//                if(res2<=0){
//                    //购物券扣除有误
//                    bools=false;
//                    System.out.println("Classify2b");
//                    Logger logger= LoggerFactory.getLogger("Classify2b"+orderNo);
//                    //throw new RuntimeException();
//                }else {
//                    //更新财务余额
//                    FinaceTotal finaceTotal4=new FinaceTotal();
//                    finaceTotal4.setfId(finaceTotal.getfId2());
//                    finaceTotal4.setOpratorId(userNoId);
//                    finaceTotal4.setBalance(((finaceTotal.getBalance2()) - amount2.doubleValue()));
//                    finaceTotal4.setChanggeTime(new Timestamp(System.currentTimeMillis()));
//                    finaceTotal4.setOpratorUId(userNoId);
//                    finaceTotal4.setUserId(userNoId);
//                    finaceTotal4.setfType("2");
//                    int cosRow;
//                    if(finaceTotal.getfId2()>0){
//                        cosRow =finaceTotalService.updateByPrimaryKeySelective(finaceTotal4);
//                    }else {
//                        cosRow =finaceTotalService.insertSelective(finaceTotal4);
//                    }
//                    if (cosRow <= 0) {
//                        bools=false;
//                        System.out.println("Classify2c");
//                        Logger logger= LoggerFactory.getLogger("Classify2c"+orderNo);
//                        //消费积分扣除有误，支付失败!
//                        //throw new RuntimeException();
//                    }
//                }
//            }
//        }
//        if(order.getCashCoupon()!=null){
//            if(order.getCashCoupon().doubleValue()>0){
//                FinaceToData finaceToData2 = new FinaceToData();
//                finaceToData2.setUserId(userNoId);//用户编号Id
//                finaceToData2.setFinaceType("111");
//                finaceToData2.setRemark("支出"+orderNo);
//                finaceToData2.setClassify(3);
//                finaceToData2.setFinaceTime(Timestamp.valueOf(date1));//时间
//                BigDecimal amount2=order.getCashCoupon();
//                finaceToData2.setAmount(amount2);//
//                finaceToData2.setBalance(BigDecimal.valueOf((finaceTotal.getBalance3()) - amount2.doubleValue()));
//                finaceToData2.setOpratorUId(userNoId);
//                int res2 = finaceService.insertFinaceData(finaceToData2);
//                if(res2<=0){
//                    bools=false;
//                    System.out.println("Classify3a");
//                    Logger logger= LoggerFactory.getLogger("Classify3a"+orderNo);
//                    //代金券扣除有误
//                    //throw new RuntimeException();
//                }else {
//                    //更新财务余额
//                    FinaceTotal finaceTotal4=new FinaceTotal();
//                    finaceTotal4.setfId(finaceTotal.getfId3());
//                    finaceTotal4.setOpratorId(userNoId);
//                    finaceTotal4.setBalance(((finaceTotal.getBalance3()) - amount2.doubleValue()));
//                    finaceTotal4.setChanggeTime(new Timestamp(System.currentTimeMillis()));
//                    finaceTotal4.setOpratorUId(userNoId);
//                    finaceTotal4.setUserId(userNoId);
//                    finaceTotal4.setfType("3");
//                    int cosRow;
//                    if(finaceTotal.getfId3()>0){
//                        cosRow =finaceTotalService.updateByPrimaryKeySelective(finaceTotal4);
//                    }else {
//                        cosRow =finaceTotalService.insertSelective(finaceTotal4);
//                    }
//                    if (cosRow <= 0) {
//                        bools=false;
//                        System.out.println("Classify3b");
//                        Logger logger= LoggerFactory.getLogger("Classify3b"+orderNo);
//                    }
//                }
//            }
//        }
//        //返还折扣券保存到数据库返finance3_table
//        Integer cashCouponMul = 0;
//        String financeName = "";
//        if(config!=null){
//            if(config.getCashcouponmul()!=null){
//                cashCouponMul = config.getCashcouponmul();//返折扣券的倍数 ---2倍
//            }
//            if(config.getFinacename3()!=""||config.getCashcouponmul()!=null){
//                financeName = config.getFinacename3();//返回----折扣券的名称
//            }
//        }
//        if(cashCouponMul>0){
//            int resf = FinaceMethods.insertFinaceU(3, BigDecimal.valueOf(order.getBackCashcoupon() * cashCouponMul), userNoId, order.getOrderNo() + "返" + financeName, userNoId, 999);
//        }
//        //更新会员等级
//        if (memberLevel == 19) {
//            userNo1.setRank(16);
//            int resu = sysUserNoService.updateByPrimaryKeySelective(userNo1);
//            if (resu <= 0) {
//                //日志or数据库
//                bools=false;
//                System.out.println("bbbb");
//                Logger logger= LoggerFactory.getLogger("bbbb"+orderNo);
//                //throw new RuntimeException();
//            }
//        }
//        order.setOrderStatus("702");
//        order.setPayStatus("1");
//        order.setPayTime(new Date());
//        Integer res3=orderService.updateByPrimaryKeySelective(order);
//        if(res3<=0){
//            bools=false;
//            System.out.println("vvv");
//            Logger logger= LoggerFactory.getLogger("vvv"+orderNo);
//            //throw new RuntimeException();
//        }
//        //添加记录运营成本到用户业绩表
//        double opratorCost=0.00;//订单运营成本
//        UserNoAchievement userNoAchievement=new UserNoAchievement();
//        userNoAchievement.setOpratorCostSign("0");//为结算的业绩标志
//        userNoAchievement.setUserNoId(memberUserId);
//        userNoAchievement.setPayTime(new Timestamp(order.getpOrderTime().getTime()));
//        if(order!=null&&order.getOpratorCost()!=null){
//            order.getOpratorCost().doubleValue();
//        }
//        userNoAchievement.setOrderId(order.getOrderId());
//        userNoAchievement.setOrderOpratorCost(opratorCost);
//        int achievementRow=userNoAchievementService.insertSelective(userNoAchievement);
//        //添加记录运营成本到用户业绩表
//
//        return bools;
//    }
        return true;
    }

    @Override
    public List<Order> selectListOrderCartByNo(String orderNo) {
        return orderMapper.selectListOrderCartByNo(orderNo);
    }

    @Override
    public void activity(Order order, String userNoId, double commission, Integer userReUserId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String date1 = sdf.format(date);

        int userNoId1=Integer.parseInt(userNoId);
//        if(commission>10){
//
//        }else {
//            userNoId1=userReUserId;
//        }
//        userNoId1=userReUserId;
        FinaceTotal finaceTotal=finaceTotalService.selectFinaceBalaceAll(userNoId1);
        FinaceTotal finaceTotalRE=finaceTotalService.selectFinaceBalaceAll(userReUserId);
        double activityInter=0,activityCostinter=0,activityCoupon=0,activityCashcoupon=0;//活动积分 消费积分 购物券 代金券
        if(order!=null){
            activityInter=order.getActivityInter()==null?0:order.getActivityInter();
            activityCoupon=order.getActivityCoupon()==null?0:order.getActivityCoupon();
            activityCashcoupon=order.getActivityCashcoupon()==null?0:order.getActivityCashcoupon();
            activityCostinter=order.getActivityCostinter()==null?0:order.getActivityCostinter();
            //System.out.println(activityInter+"---"+activityCoupon+"---"+activityCashcoupon);
            if(activityInter>0){
//                double balance1=balance+activityInter;
                double balance1=finaceTotalRE.getBalance1()+activityInter;
                FinaceToData finaceToData = new FinaceToData();
                finaceToData.setUserId(userReUserId);//用户编号Id
                finaceToData.setFinaceType("222");
                finaceToData.setRemark("活动收入"+order.getOrderNo());
                finaceToData.setClassify(1);
                finaceToData.setFinaceTime(Timestamp.valueOf(date1));//时间
                finaceToData.setAmount(BigDecimal.valueOf(activityInter));//
                finaceToData.setBalance(BigDecimal.valueOf(balance1));
                int res2 = finaceService.insertFinaceData(finaceToData);
                if(res2<=0){

                }else {
                    //更新财务余额
                    FinaceTotal finaceTotal1=new FinaceTotal();
                    //System.out.println("fid1:"+finaceTotal.getfId1());
                    finaceTotal1.setOpratorId(userReUserId);
                    finaceTotal1.setBalance(balance1);
                    finaceTotal1.setChanggeTime(new Timestamp(System.currentTimeMillis()));
                    finaceTotal1.setOpratorUId(userReUserId);
                    finaceTotal1.setUserId(userReUserId);
                    finaceTotal1.setfType("1");
                    int cosRow;
                    if(finaceTotalRE.getfId1()>0){
                        finaceTotal1.setfId(finaceTotalRE.getfId1());
                        cosRow =finaceTotalService.updateByPrimaryKeySelective(finaceTotal1);
                        System.out.println("s1");
                    }else {
                        cosRow =finaceTotalService.insertSelective(finaceTotal1);
                        System.out.println("s2");
                    }
                    if (cosRow <= 0) {
                    }
                }
            }
            if(activityCoupon>0){
                FinaceToData finaceToData2 = new FinaceToData();
                finaceToData2.setUserId(userNoId1);//用户编号Id
                finaceToData2.setFinaceType("222");
                finaceToData2.setRemark("活动收入"+order.getOrderNo());
                finaceToData2.setClassify(2);
                finaceToData2.setFinaceTime(Timestamp.valueOf(date1));//时间
                finaceToData2.setAmount(BigDecimal.valueOf(activityCoupon));//
                finaceToData2.setBalance(BigDecimal.valueOf((finaceTotal.getBalance2()) + activityCoupon));
                //finaceToData2.setOpratorUId(userNoId);
                int res2 = finaceService.insertFinaceData(finaceToData2);
                if(res2<=0){

                }else {
                    //更新财务余额
                    FinaceTotal finaceTotal2=new FinaceTotal();
                    //finaceTotal4.setOpratorId(userNoId1);
                    finaceTotal2.setBalance(((finaceTotal.getBalance2()) + activityCoupon));
                    finaceTotal2.setChanggeTime(new Timestamp(System.currentTimeMillis()));
                    //finaceTotal4.setOpratorUId(userNoId);
                    finaceTotal2.setUserId(userNoId1);
                    finaceTotal2.setfType("2");
                    int cosRow;
                    if(finaceTotal.getfId2()>0){
                        finaceTotal2.setfId(finaceTotal.getfId2());
                        cosRow =finaceTotalService.updateByPrimaryKeySelective(finaceTotal2);
                        System.out.println("a1");
                    }else {
                        System.out.println("a2");
                        cosRow =finaceTotalService.insertSelective(finaceTotal2);
                    }
                    if (cosRow <= 0) {
                    }
                }
            }
            if(activityCashcoupon>0){
                FinaceToData finaceToData2 = new FinaceToData();
                finaceToData2.setUserId(userNoId1);//用户编号Id
                finaceToData2.setFinaceType("222");
                finaceToData2.setRemark("活动收入"+order.getOrderNo());
                finaceToData2.setClassify(3);
                finaceToData2.setFinaceTime(Timestamp.valueOf(date1));//时间
                finaceToData2.setAmount(BigDecimal.valueOf(activityCashcoupon));//
                finaceToData2.setBalance(BigDecimal.valueOf((finaceTotal.getBalance3()) + activityCashcoupon));
                //finaceToData2.setOpratorUId(userNoId);
                int res2 = finaceService.insertFinaceData(finaceToData2);
                if(res2<=0){

                }else {
                    //更新财务余额
                    FinaceTotal finaceTotal3=new FinaceTotal();
                    //finaceTotal4.setOpratorId(userNoId1);
                    finaceTotal3.setBalance(((finaceTotal.getBalance3()) + activityCashcoupon));
                    finaceTotal3.setChanggeTime(new Timestamp(System.currentTimeMillis()));
                    //finaceTotal4.setOpratorUId(userNoId);
                    finaceTotal3.setUserId(userNoId1);
                    finaceTotal3.setfType("3");
                    int cosRow;
                    if(finaceTotal.getfId3()>0){
                        System.out.println("d1");
                        finaceTotal3.setfId(finaceTotal.getfId3());
                        cosRow =finaceTotalService.updateByPrimaryKeySelective(finaceTotal3);
                    }else {
                        System.out.println("d2");
                        cosRow =finaceTotalService.insertSelective(finaceTotal3);
                    }
                    if (cosRow <= 0) {
                    }
                }
            }

            if(activityCostinter>0){
                FinaceToData finaceToData2 = new FinaceToData();
                finaceToData2.setUserId(userNoId1);//用户编号Id
                finaceToData2.setFinaceType("222");
                finaceToData2.setRemark("活动收入"+order.getOrderNo());
                finaceToData2.setClassify(4);
                finaceToData2.setFinaceTime(Timestamp.valueOf(date1));//时间
                finaceToData2.setAmount(BigDecimal.valueOf(activityCostinter));//
                finaceToData2.setBalance(BigDecimal.valueOf((finaceTotal.getBalance4()) + activityCostinter));
                //finaceToData2.setOpratorUId(userNoId);
                int res2 = finaceService.insertFinaceData(finaceToData2);
                if(res2<=0){

                }else {
                    //更新财务余额
                    FinaceTotal finaceTotal3=new FinaceTotal();
                    //finaceTotal4.setOpratorId(userNoId1);
                    finaceTotal3.setBalance(((finaceTotal.getBalance4()) + activityCostinter));
                    finaceTotal3.setChanggeTime(new Timestamp(System.currentTimeMillis()));
                    //finaceTotal4.setOpratorUId(userNoId);
                    finaceTotal3.setUserId(userNoId1);
                    finaceTotal3.setfType("4");
                    int cosRow;
                    if(finaceTotal.getfId4()>0){
                        System.out.println("d1");
                        finaceTotal3.setfId(finaceTotal.getfId4());
                        cosRow =finaceTotalService.updateByPrimaryKeySelective(finaceTotal3);
                    }else {
                        System.out.println("d2");
                        cosRow =finaceTotalService.insertSelective(finaceTotal3);
                    }
                    if (cosRow <= 0) {
                    }
                }
            }

        }
    }

    @Override
    public void activity(Order order, String userNoId, double commission, Integer userReUserId, double balance) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String date1 = sdf.format(date);

        int userNoId1=Integer.parseInt(userNoId);
//        if(commission>10){
//
//        }else {
//            userNoId1=userReUserId;
//        }
        //userNoId1=userReUserId;
        FinaceTotal finaceTotal=finaceTotalService.selectFinaceBalaceAll(userNoId1);
        FinaceTotal finaceTotalRE=finaceTotalService.selectFinaceBalaceAll(userReUserId);
        double activityInter=0,activityCostinter=0,activityCoupon=0,activityCashcoupon=0;//活动积分 消费积分 购物券 代金券
        if(order!=null){
            activityInter=order.getActivityInter()==null?0:order.getActivityInter();
            activityCoupon=order.getActivityCoupon()==null?0:order.getActivityCoupon();
            activityCashcoupon=order.getActivityCashcoupon()==null?0:order.getActivityCashcoupon();
            activityCostinter=order.getActivityCostinter()==null?0:order.getActivityCostinter();
            //System.out.println(activityInter+"---"+activityCoupon+"---"+activityCashcoupon);
            if(activityInter>0){
//                double balance1=balance+activityInter;
                double balance1=finaceTotalRE.getBalance1()+activityInter;
                FinaceToData finaceToData = new FinaceToData();
                finaceToData.setUserId(userReUserId);//用户编号Id
                finaceToData.setFinaceType("222");
                finaceToData.setRemark("活动收入"+order.getOrderNo());
                finaceToData.setClassify(1);
                finaceToData.setFinaceTime(Timestamp.valueOf(date1));//时间
                finaceToData.setAmount(BigDecimal.valueOf(activityInter));//
                finaceToData.setBalance(BigDecimal.valueOf(balance1));
                int res2 = finaceService.insertFinaceData(finaceToData);
                if(res2<=0){

                }else {
                    //更新财务余额
                    FinaceTotal finaceTotal1=new FinaceTotal();
                    //System.out.println("fid1:"+finaceTotal.getfId1());
                    finaceTotal1.setOpratorId(userReUserId);
                    finaceTotal1.setBalance(balance1);
                    finaceTotal1.setChanggeTime(new Timestamp(System.currentTimeMillis()));
                    finaceTotal1.setOpratorUId(userReUserId);
                    finaceTotal1.setUserId(userReUserId);
                    finaceTotal1.setfType("1");
                    int cosRow;
                    if(finaceTotalRE.getfId1()>0){
                        finaceTotal1.setfId(finaceTotalRE.getfId1());
                        cosRow =finaceTotalService.updateByPrimaryKeySelective(finaceTotal1);
                        System.out.println("s1");
                    }else {
                        cosRow =finaceTotalService.insertSelective(finaceTotal1);
                        System.out.println("s2");
                    }
                    if (cosRow <= 0) {
                    }
                }
            }
            if(activityCoupon>0){
                FinaceToData finaceToData2 = new FinaceToData();
                finaceToData2.setUserId(userNoId1);//用户编号Id
                finaceToData2.setFinaceType("222");
                finaceToData2.setRemark("活动收入"+order.getOrderNo());
                finaceToData2.setClassify(2);
                finaceToData2.setFinaceTime(Timestamp.valueOf(date1));//时间
                finaceToData2.setAmount(BigDecimal.valueOf(activityCoupon));//
                finaceToData2.setBalance(BigDecimal.valueOf((finaceTotal.getBalance2()) + activityCoupon));
                //finaceToData2.setOpratorUId(userNoId);
                int res2 = finaceService.insertFinaceData(finaceToData2);
                if(res2<=0){

                }else {
                    //更新财务余额
                    FinaceTotal finaceTotal2=new FinaceTotal();
                    //finaceTotal4.setOpratorId(userNoId1);
                    finaceTotal2.setBalance(((finaceTotal.getBalance2()) + activityCoupon));
                    finaceTotal2.setChanggeTime(new Timestamp(System.currentTimeMillis()));
                    //finaceTotal4.setOpratorUId(userNoId);
                    finaceTotal2.setUserId(userNoId1);
                    finaceTotal2.setfType("2");
                    int cosRow;
                    if(finaceTotal.getfId2()>0){
                        finaceTotal2.setfId(finaceTotal.getfId2());
                        cosRow =finaceTotalService.updateByPrimaryKeySelective(finaceTotal2);
                        System.out.println("a1");
                    }else {
                        System.out.println("a2");
                        cosRow =finaceTotalService.insertSelective(finaceTotal2);
                    }
                    if (cosRow <= 0) {
                    }
                }
            }
            if(activityCashcoupon>0){
                FinaceToData finaceToData2 = new FinaceToData();
                finaceToData2.setUserId(userNoId1);//用户编号Id
                finaceToData2.setFinaceType("222");
                finaceToData2.setRemark("活动收入"+order.getOrderNo());
                finaceToData2.setClassify(3);
                finaceToData2.setFinaceTime(Timestamp.valueOf(date1));//时间
                finaceToData2.setAmount(BigDecimal.valueOf(activityCashcoupon));//
                finaceToData2.setBalance(BigDecimal.valueOf((finaceTotal.getBalance3()) + activityCashcoupon));
                //finaceToData2.setOpratorUId(userNoId);
                int res2 = finaceService.insertFinaceData(finaceToData2);
                if(res2<=0){

                }else {
                    //更新财务余额
                    FinaceTotal finaceTotal3=new FinaceTotal();
                    //finaceTotal4.setOpratorId(userNoId1);
                    finaceTotal3.setBalance(((finaceTotal.getBalance3()) + activityCashcoupon));
                    finaceTotal3.setChanggeTime(new Timestamp(System.currentTimeMillis()));
                    //finaceTotal4.setOpratorUId(userNoId);
                    finaceTotal3.setUserId(userNoId1);
                    finaceTotal3.setfType("3");
                    int cosRow;
                    if(finaceTotal.getfId3()>0){
                        System.out.println("d1");
                        finaceTotal3.setfId(finaceTotal.getfId3());
                        cosRow =finaceTotalService.updateByPrimaryKeySelective(finaceTotal3);
                    }else {
                        System.out.println("d2");
                        cosRow =finaceTotalService.insertSelective(finaceTotal3);
                    }
                    if (cosRow <= 0) {
                    }
                }
            }
            if(activityCostinter>0){
                FinaceToData finaceToData2 = new FinaceToData();
                finaceToData2.setUserId(userNoId1);//用户编号Id
                finaceToData2.setFinaceType("222");
                finaceToData2.setRemark("活动收入"+order.getOrderNo());
                finaceToData2.setClassify(4);
                finaceToData2.setFinaceTime(Timestamp.valueOf(date1));//时间
                finaceToData2.setAmount(BigDecimal.valueOf(activityCostinter));//
                finaceToData2.setBalance(BigDecimal.valueOf((finaceTotal.getBalance4()) + activityCostinter));
                //finaceToData2.setOpratorUId(userNoId);
                int res2 = finaceService.insertFinaceData(finaceToData2);
                if(res2<=0){

                }else {
                    //更新财务余额
                    FinaceTotal finaceTotal3=new FinaceTotal();
                    //finaceTotal4.setOpratorId(userNoId1);
                    finaceTotal3.setBalance(((finaceTotal.getBalance4()) + activityCostinter));
                    finaceTotal3.setChanggeTime(new Timestamp(System.currentTimeMillis()));
                    //finaceTotal4.setOpratorUId(userNoId);
                    finaceTotal3.setUserId(userNoId1);
                    finaceTotal3.setfType("4");
                    int cosRow;
                    if(finaceTotal.getfId4()>0){
                        finaceTotal3.setfId(finaceTotal.getfId4());
                        cosRow =finaceTotalService.updateByPrimaryKeySelective(finaceTotal3);
                    }else {
                        cosRow =finaceTotalService.insertSelective(finaceTotal3);
                    }
                    if (cosRow <= 0) {
                    }
                }
            }
        }
    }
}
